﻿using Sidvall.Security;

namespace Sidvall.Data
{
    public class CopySettings
    {
        #region Public Members

        public Sidvall.Business.FieldCopyOptions FieldCopyOption { get; set; }
        public bool IncludeChildren { get; set; }
        public bool IncludeParents { get; set; }
        public bool RaisePropertyChanged { get; set; }
        public Sidvall.Data.MergeOption MergeOption { get; set; }
        public Sidvall.Data.DataRowFilters RowFilter { get; set; }
        public Sidvall.Data.DataRowState? DataRowState { get; set; }
        public Sidvall.Data.DataRowContextOption DataRowContextOption { get; set; }

        #region IncludedProperty

        public bool IncludedProperty(IPropertyAccessLevelProvider accessLevelProvider, bool isKeyProperty, bool propertyIsInRelation, bool isRelationProperty,
            Sidvall.Data.DataType propertyDataType, string propertyName, string entityName)
        {
            if (_Evaluator != null)
                return _Evaluator.CanAccess(accessLevelProvider, isKeyProperty, propertyIsInRelation, isRelationProperty, propertyDataType, propertyName, entityName);
            return true;
        }

        #endregion
        #region SetEvaluator

        public void SetEvaluator(IPropertyAccessLevelEvaluator accessLevelEvaluator)
        {
            _Evaluator = accessLevelEvaluator;
        }

        #endregion

        #endregion
        #region Factory Members

        #region Clone

        public static CopySettings Clone()
        {
            return Clone(false, null);
        }
        public static CopySettings Clone(bool raisePropertyChanged)
        {
            return Clone(raisePropertyChanged, null);
        }
        public static CopySettings Clone(bool raisePropertyChanged, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullCopyAll(DataRowContextOption.StateAndExistingKey, dataRowState, Business.FieldCopyOptions.All, raisePropertyChanged);
        }

        #endregion
        #region FullCopyAll

        public static CopySettings FullCopyAll()
        {
            return FullCopyAll(DataRowContextOption.None, null);
        }
        public static CopySettings FullCopyAll(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullCopyAll(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings FullCopyAll(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return FullCopyAll(dataRowContextOption, dataRowState, fieldOption, false);
        }
        public static CopySettings FullCopyAll(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption,
            bool raisePropertyChanged)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, true, true, MergeOption.Append, DataRowFilters.All, raisePropertyChanged);
        }

        #endregion
        #region FullCopyCurrent

        public static CopySettings FullCopyCurrent()
        {
            return FullCopyCurrent(DataRowContextOption.None, null);
        }
        public static CopySettings FullCopyCurrent(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullCopyCurrent(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings FullCopyCurrent(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, true, true, MergeOption.Append, DataRowFilters.Current, false);
        }

        #endregion
        #region FullCopyModified

        public static CopySettings FullCopyModified()
        {
            return FullCopyModified(DataRowContextOption.None, null);
        }
        public static CopySettings FullCopyModified(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullCopyModified(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings FullCopyModified(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, true, true, MergeOption.Append, DataRowFilters.Modified, false);
        }

        #endregion
        #region FullMergeAll

        public static CopySettings FullMergeAll()
        {
            return FullMergeAll(DataRowContextOption.None, null);
        }
        public static CopySettings FullMergeAll(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullMergeAll(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings FullMergeAll(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return FullMergeAll(dataRowContextOption, dataRowState, fieldOption, false);
        }
        public static CopySettings FullMergeAll(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption,
            bool raisePropertyChanged)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, true, true, MergeOption.Merge, DataRowFilters.All, raisePropertyChanged);
        }

        #endregion
        #region FullMergeCurrent

        public static CopySettings FullMergeCurrent()
        {
            return FullMergeCurrent(DataRowContextOption.None, null);
        }
        public static CopySettings FullMergeCurrent(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullMergeCurrent(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings FullMergeCurrent(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, true, true, MergeOption.Merge, DataRowFilters.Current, false);
        }

        #endregion
        #region FullMergeModified

        public static CopySettings FullMergeModified()
        {
            return FullMergeModified(DataRowContextOption.None, null);
        }
        public static CopySettings FullMergeModified(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return FullMergeModified(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings FullMergeModified(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, true, true, MergeOption.Merge, DataRowFilters.Modified, false);
        }

        #endregion
        #region ItemCopy

        public static CopySettings ItemCopy()
        {
            return ItemCopy(DataRowContextOption.None, null);
        }
        public static CopySettings ItemCopy(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
        {
            return ItemCopy(dataRowContextOption, dataRowState, Business.FieldCopyOptions.All);
        }
        public static CopySettings ItemCopy(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption)
        {
            return ItemCopy(dataRowContextOption, dataRowState, fieldOption, false);
        }
        public static CopySettings ItemCopy(Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.FieldCopyOptions fieldOption,
            bool raisePropertyChanged)
        {
            return new CopySettings(fieldOption, dataRowContextOption, dataRowState, false, false, MergeOption.Append, DataRowFilters.Current, raisePropertyChanged);
        }

        #endregion

        #endregion
        #region Private Members

        private IPropertyAccessLevelEvaluator _Evaluator;

        #endregion
        #region Constructors

        public CopySettings()
            : this(Business.FieldCopyOptions.All)
        {
        }
        public CopySettings(Sidvall.Data.DataRowContextOption dataRowContextOption)
            : this(Business.FieldCopyOptions.All, dataRowContextOption, null)
        {
        }

        public CopySettings(Sidvall.Business.FieldCopyOptions fieldOption)
            : this(fieldOption, DataRowContextOption.None, null)
        {
        }


        public CopySettings(Sidvall.Business.FieldCopyOptions fieldOption, Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState)
            : this(fieldOption, dataRowContextOption, dataRowState, false, false, MergeOption.Append, DataRowFilters.Current, false)
        {
        }

        public CopySettings(Sidvall.Business.FieldCopyOptions fieldOption, Sidvall.Data.DataRowContextOption dataRowContextOption, Sidvall.Data.DataRowState? dataRowState,
            bool includeChildren, bool includeParents, Sidvall.Data.MergeOption mergeOption, Sidvall.Data.DataRowFilters rowFilter, bool raisePropertyChanged)
        {
            this.FieldCopyOption = fieldOption;
            this.IncludeChildren = includeChildren;
            this.IncludeParents = includeParents;
            this.MergeOption = mergeOption;
            this.RowFilter = rowFilter;
            this.DataRowState = dataRowState;
            this.DataRowContextOption = dataRowContextOption;
            this.RaisePropertyChanged = raisePropertyChanged;
        }

        #endregion
    }
}
